package com.xiaoyu.lanling.media.image

import `in`.srain.cube.util.LocalDisplay
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.dp
import com.xiaoyu.lanling.R

class UserImageLoadParam private constructor(builder: UserImageLoadParamBuilder) : ImageLoadParam(builder) {

    val user = builder.mUser

    class UserImageLoadParamBuilder : ImageLoadParamBuilder<UserImageLoadParamBuilder?>() {
        var mUser: User? = null
        var mNeedDefaultAvatar = true

        fun setUserAndDP(user: User, dp: Int): UserImageLoadParamBuilder {
            mUser = user

            if (mNeedDefaultAvatar) {
                setPlaceHolderResId(R.drawable.icon_user_default);
            } else {
                setEnableEmptyUrl(true)
            }
            setWidthInDP(dp)
            setCornersRadius(8.dp)
            setUrl(user.avatar)
            return this
        }

        fun setUserAndDPRound(user: User, dp: Int): UserImageLoadParamBuilder {
            mUser = user
            if (mNeedDefaultAvatar) {
                setPlaceHolderResId(R.drawable.icon_user_default);
            } else {
                setEnableEmptyUrl(true)
            }
            setWidthInDP(dp)
            setRounded(true)
            setUrl(user.avatar)
            return this
        }

        fun setUser(user: User, widthInDp: Int, heightInDp: Int): UserImageLoadParamBuilder {
            mUser = user
            setWidthInDP(widthInDp)
            setHeightInDP(heightInDp)
            setUrl(user.avatar)
            return this
        }

        fun setUserInfoAndDPRoundWithBorder(user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int, needDefaultAvatar: Boolean): UserImageLoadParamBuilder {
            mNeedDefaultAvatar = needDefaultAvatar
            return setUserInfoAndDPRoundWithBorder(user, dp, borderWidthDP, borderColorRes)
        }

        fun setUserInfoAndDPWithBorder(user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int, needDefaultAvatar: Boolean): UserImageLoadParamBuilder {
            mNeedDefaultAvatar = needDefaultAvatar
            return setUserInfoAndDPWithBorder(user, dp, borderWidthDP, borderColorRes)
        }

        private fun setUserInfoAndDPRoundWithBorder(user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int): UserImageLoadParamBuilder {
            this.setUserAndDPRound(user, dp)
            setRoundingBorderWidth(LocalDisplay.dp2px(borderWidthDP.toFloat()))
            setRoundingBorderColor(AppContext.getColor(borderColorRes))
            return this
        }

        private fun setUserInfoAndDPWithBorder(user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int): UserImageLoadParamBuilder {
            this.setUserAndDP(user, dp)
            setRoundingBorderWidth(LocalDisplay.dp2px(borderWidthDP.toFloat()))
            setRoundingBorderColor(AppContext.getColor(borderColorRes))
            return this
        }

        override fun build(): UserImageLoadParam {
            checkNotNull(mUser) { "user required" }
            processParam()
            return UserImageLoadParam(this)
        }
    }

    companion object {
        @JvmStatic
        fun newBuilder(): UserImageLoadParamBuilder {
            return UserImageLoadParamBuilder()
        }
    }
}